{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# 获取前n个主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100,2))\n",
    "X[:, 0] = np.random.uniform(0., 100., size=100)\n",
    "X[:, 1] = 0.75 * X[:,0] + 3. + np.random.normal(0, 10., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(X):\n",
    "    return X - np.mean(X, axis=0)\n",
    "\n",
    "X = demean(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4xLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvAOZPmwAAGnBJREFUeJzt3X+MXXWZx/HPwzDWYc3uoFSE23bbRAIpoilMWJLuH0s1FuVHGzAE16yoJI0JZnWXFKfwB7vJGsY0/ow/kkZNMEsCrLClK2wq0v6xS1J0Sq0EoW5XFBlBapaq2Xa1Lc/+cc8td+7cc3+d77nnx/f9Skjn/pi539Ohz/me5/t8n2PuLgBA/Z1R9AAAAONBwAeASBDwASASBHwAiAQBHwAiQcAHgEgQ8AEgEgR8AIgEAR8AInFm0QNod8455/jq1auLHgYAVMr+/ft/4+7L+72vVAF/9erVmp+fL3oYAFApZvaLQd5HSgcAIkHAB4BIEPABIBIEfACIBAEfACJRqiodAIjNzgML2r77kH519LjOn57S1o0XavO6Ri6fRcAHgILsPLC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figure at 0x106494198>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:,0], X[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, X):\n",
    "    return np.sum((X.dot(w)**2)) / len(X)\n",
    "\n",
    "def df(w, X):\n",
    "    return X.T.dot(X.dot(w)) * 2. /len(X)\n",
    "\n",
    "def direction(w):\n",
    "    return w / np.linalg.norm(w)\n",
    "\n",
    "def first_component(X, initial_w, eta, n_iters=1e4, epsilon=1e-8):\n",
    "    w = direction(initial_w)\n",
    "    cur_iter = 0\n",
    "    \n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w, X)\n",
    "        last_w = w\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w)\n",
    "        if (abs(f(w,X) - f(last_w, X)) < epsilon):\n",
    "            break\n",
    "        cur_iter += 1\n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.765973  ,  0.64287275])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "initial_w = np.random.random(X.shape[1])\n",
    "eta = 0.01\n",
    "w = first_component(X, initial_w, eta)\n",
    "w"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 去掉在第一主成分上的分量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.empty(X.shape)\n",
    "for i in range(len(X)):\n",
    "    X2[i] = X[i] - X[i].dot(w)*w\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "数据点变成这样了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUh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7z7JF0k5m9w8yWaKat4y8jHlMoir8AJddr5kZ2Uv1K0lIzW2JmZ2nmxvyeiMcUOjM7x8zeWfpa0lVK9s95tj2SPlH8+hOSEvGXPTP8OpnZ9ZK+JWmhZto6jrn7gLsfMrNdkn4t6W1Jn3P36SjHGqKvm1mfZpZ0XpL0mWiHEx53f9vMbpE0LKlD0r3ufijiYbXC+ZIeMTNpJk58z91/Gu2QwmFmD0q6XNJ5xZatmyRtlbTLzD6lmQq+N0Y3wuZhpy0ApARLOgCQEgR8AEgJAj4ApAQBHwBSgoAPAClBwAeAlCDgA0BKEPABICX+H7Qc3HkWBbAZAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x106552320>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.64287731, -0.76596917])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2 = first_component(X2, initial_w, eta)\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.9576837100894231e-06"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# w向量与w2向量的点乘，应该是0（因为两个方向垂直）\n",
    "w.dot(w2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 求X2的过程改为向量化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = X - X.dot(w).reshape(-1,1)*w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAA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3Akk7z7JF0k5m9w8yWaKat4y8jHlMoir8AJddr5kZ2Uv1K0lIzW2JmZ2nmxvyeiMcUOjM7x8zeWfpa0lVK9s95tj2SPlH8+hOSEvGXPTP8OpnZ9ZK+JWmhZto6jrn7gLsfMrNdkn4t6W1Jn3P36SjHGqKvm1mfZpZ0XpL0mWiHEx53f9vMbpE0LKlD0r3ufijiYbXC+ZIeMTNpJk58z91/Gu2QwmFmD0q6XNJ5xZatmyRtlbTLzD6lmQq+N0Y3wuZhpy0ApARLOgCQEgR8AEgJAj4ApAQBHwBSgoAPAClBwAeAlCDgA0BKEPABICX+H7Qc3HkWBbAZAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x106692470>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:,0], X2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把前面所有，合成一个函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def first_n_components(n, X, eta=0.01, n_iters=1e4, epsilon=1e-8):\n",
    "    X_pca = X.copy()\n",
    "    X_pca = demean(X_pca)\n",
    "    res = []\n",
    "    for i in range(n):\n",
    "        initial_w = np.random.random(X_pca.shape[1])\n",
    "        w = first_component(X_pca, initial_w, eta)\n",
    "        res.append(w)\n",
    "        \n",
    "        X_pca = X_pca - X_pca.dot(w).reshape(-1,1)*w\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([ 0.76597294,  0.64287282]), array([ 0.64287506, -0.76597106])]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_n_components(2, X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
